# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.historyDao import HistoryDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertHistory', methods=['POST'])
def insert_historyNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'userId': request.json.get('userId', ''),
        'userIp': request.json.get('userIp', ''),
        'hisKeywords': request.json.get('hisKeywords', ''),
        'hisType': request.json.get('hisType', ''),
        'hisSearchCount': request.json.get('hisSearchCount', '')
    }

    info = HistoryDao().insertHis(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findHistoryById', methods=['POST'])
def find_history_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    hisId = request.json.get('hisId', '')
    info = HistoryDao().findHisById(hisId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/queryHistoryByCondition', methods=['POST'])
def find_history_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = HistoryDao().findHisByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                  multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 0, "status": "error"}})


@app.route('/deleteHistoryByIds', methods=['POST'])
def delete_history_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [])

    info = HistoryDao().deleteHis(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateHistoryById', methods=['POST'])
def update_history_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        hisId = request.json['hisId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入修改信息的ID"}})
    oldInfo = HistoryDao().selectHis(hisId)
    info = {
        'hisId': request.json['hisId'],
        'userId': request.json.get('userId', oldInfo[1]),
        'userIp': request.json.get('userIp', oldInfo[2]),
        'hisKeywords': request.json.get('hisKeywords', oldInfo[3]),
        'hisType': request.json.get('hisType', oldInfo[4]),
        'hisSearchCount': request.json.get('hisSearchCount', oldInfo[5])
    }

    info = HistoryDao().updateHis(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/queryHistory', methods=['POST'])
def queryHistoryNoToken():
    """
    根据ID查询，返回5条历史记录
    :return:
    """
    if not request.json:
        abort(400)
    try:
        userId = request.json['userId']
    except:
        return jsonify({"message": {"code": 0, "status": "需要参数userId"}})

    info = HistoryDao().queryHistoryLimit5(userId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})


@app.route('/clearHistorys', methods=['POST'])
def clearHistoryNoToken():
    if not request.json:
        abort(400)
    try:
        userId = request.json['userId']
    except:
        return jsonify({"message": {"code": 0, "status": "缺少参数userId"}})

    rsData = HistoryDao().clearHistorys(userId)

    if rsData:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
